package org.yuanqiframework.yuanqi.boot.sys.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.boot.sys.dto.SysDeptDTO;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysDept;
import org.yuanqiframework.yuanqi.boot.sys.service.SysDeptService;
import org.yuanqiframework.yuanqi.boot.sys.vo.SysDeptVO;
import org.yuanqiframework.yuanqi.core.utils.BeanUtils;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/dept")
public class DeptController extends BaseController {
    @Resource
    private SysDeptService sysDeptService;


    @PostMapping("save")
    public AjaxResult<?> save(@Valid @RequestBody SysDeptDTO sysDeptDTO) {
        log.debug("添加部门：{}", sysDeptDTO);
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptDTO, sysDept);
        return toAjax(sysDeptService.saveDept(sysDept));
    }


    @PostMapping("update/{id}")
    public AjaxResult<?> update(@PathVariable String id, @Valid @RequestBody SysDeptDTO sysDeptDTO) {
        log.debug("更新部门：{}", sysDeptDTO);
        SysDept sysDept = new SysDept();
        sysDept.setId(id);
        BeanUtils.copyProperties(sysDeptDTO, sysDept);
        return toAjax(sysDeptService.updateDept(sysDept));
    }


    @PostMapping("remove/{id}")
    public AjaxResult<?> remove(@PathVariable String id) {
        log.info("删除部门：{}", id);
        return toAjax(sysDeptService.removeDept(id));
    }

    @GetMapping("list")
    public AjaxResult<List<SysDeptVO>> list() {
        return success(sysDeptService.listDept());
    }

}
